﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.IO;

public enum TraceTypes
{
    Info,
    Warning,
    Error,
}

namespace bind_n_find
{
    static class BNFTrace
    {
        #region constants

        private const string LOGFILENAME="bind-n-find.log";
        private const string FILEPREFIX = "file:\\";
        private const string LOGDIRNAME = "log";


        #endregion //constants

        #region construction and initialization

        /// <summary>
        /// Tracing 
        /// </summary>
        static BNFTrace()
        {
            try
            {
                string path=checkLogFiles();
                TextWriterTraceListener listener = new TextWriterTraceListener(path);
                Trace.Listeners.Add(listener);
                Trace.AutoFlush = true;
            }
            catch (Exception ex) { };
        }

        /// <summary>
        /// Check if log file already exists. If exists, try to change its name to name + ".N" to save previous logs
        /// </summary>
        private static string checkLogFiles()
        {
            string filePath = LOGFILENAME;
            try
            {
                string dirPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                if (dirPath.Contains(FILEPREFIX))
                {
                    dirPath = dirPath.Replace(FILEPREFIX, string.Empty);
                }
                dirPath = Path.Combine(dirPath, LOGDIRNAME);
                if (!Directory.Exists(dirPath))
                    Directory.CreateDirectory(dirPath);
                filePath = Path.Combine(dirPath, LOGFILENAME);
                string filePath1 = filePath;
                int i = 1;
                while (File.Exists(filePath1))
                {
                    filePath1 = filePath + "." + i.ToString();
                    ++i;
                }
                if (filePath != filePath1)
                {
                    File.Move(filePath, filePath1);
                }
                
            }
            catch(Exception ex)
            {
                return filePath;
            }
            return filePath;
        }

        #endregion //construction and initialization

        #region public methods

        /// <summary>
        /// Trace to trace listeners 
        /// </summary>
        /// <param name="traceType"></param>
        /// <param name="msg"></param>
        public static void DoTrace(TraceTypes traceType, string msg)
        {
            try
            {
                switch (traceType)
                {
                    case TraceTypes.Error:
                        Trace.TraceError(msg);
                        break;
                    case TraceTypes.Warning:
                        Trace.TraceWarning(msg);
                        break;
                    default:
                        Trace.TraceInformation(msg);
                        break;

                }
            }
            catch (Exception ex)
            {
            }
            
        }

        #endregion //public methods


    }
}
